package com.zmn.oms.dubbo.impl.discount;

import com.alibaba.fastjson.JSON;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.dms.common.dto.coup.dio.coupon.CoupEngineerDIO;
import com.zmn.dms.dubbo.interfaces.coup.coupon.CouponListRemoteService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.dio.discount.UserCoupQuery;
import com.zmn.oms.dubbo.interfaces.discount.OrderUserDiscountListRemoterService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.discount.OrderDiscountService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * @author sunlife
 * @date:2022/4/14 下午5:24
 * description:
 */
@Slf4j
@DubboService(loadbalance = "roundrobin", timeout = 10000, retries = 0, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class OrderUserDiscountListRemoterServiceImpl implements OrderUserDiscountListRemoterService {

    @Autowired
    OrderWorkService orderWorkService;
    @Autowired
    OrderDiscountService orderDiscountService;

    @DubboReference(version = com.zmn.dms.dubbo.utils.DubboConsts.INTERFACE_VERSION, check = false)
    CouponListRemoteService couponListRemoteService;

    @Override
    public ResponseDTO getOrderDiscountAndUserDiscountList(UserCoupQuery userCoupQuery) {
        // 获取工单优惠信息
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(userCoupQuery.getOrderId(), userCoupQuery.getWorkId());
        if (orderWork == null) {
            return ResponseDTO.fail("工单不存在");
        }

        CoupEngineerDIO query = BeanMapper.map(userCoupQuery, CoupEngineerDIO.class);
        if (NumberUtil.isNotNullOrZero(orderWork.getUserId())) {
            query.setUserId(orderWork.getUserId());
        }
        if (NumberUtil.isNotNullOrZero(orderWork.getVisitUserId())) {
            query.setScanUserId(orderWork.getVisitUserId());
        }
        query.setOrderTime(orderWork.getReceiveTime());
        query.setOrderId(orderWork.getOrderId());
        query.setChannelId(orderWork.getChannelId());
        query.setCityId(orderWork.getCityId());
        query.setProductId(orderWork.getShowProductId());
        query.setPrice(orderWork.getOriginalAmount());
        query.setBizType(orderWork.getBizType());

        int tariff = Objects.equals(orderWork.getServItemType(), OrderConsts.SERVICE_ITEM_TYPE_FAULT) ? GlobalConsts.YES : GlobalConsts.NO;
        query.setTariffOnoff(tariff);
        query.setUntariffOnoff(tariff == GlobalConsts.YES ? GlobalConsts.NO : GlobalConsts.YES);
        int extendedWarranty = orderWork.getMember() != null ? orderWork.getMember() : GlobalConsts.NO;
        query.setExtendedWarranty(extendedWarranty);
        query.setUnExtendedWarranty(Objects.equals(extendedWarranty, GlobalConsts.YES) ? GlobalConsts.NO : GlobalConsts.YES);

        log.info("获取用户使用的优惠券 入参：{}", JSON.toJSONString(query));
        return couponListRemoteService.listPageCoupCouponByQuery(query);
    }
}
